package com.hgyc.mom.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.common.entity.SysDictItem;
import com.hgyc.mom.common.exception.BizException;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.system.service.SysDictItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 字典项 前端控制器
 * </p>
 *
 * @author fwj
 * @since 2025-01-16
 */
@RestController
@RequestMapping("/sys/dictItem")
public class SysDictItemController {

    @Autowired
    private SysDictItemService sysDictItemService;

    @RequestMapping(value = "/list/{dictId}", method = RequestMethod.GET)
    @Operation(summary = "获取字典项列表", description = "获取字典项列表")
    public Result<List<SysDictItem>> list(
            @Parameter(description = "查询参数") @PathVariable("dictId") Long dictId) {
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper<>();
        if (dictId == null) {
            throw new BizException("字典ID不能为空");
        } else {
            wrapper.eq(SysDictItem::getDictId, dictId);
        }
        wrapper.orderByAsc(SysDictItem::getSort);
        List<SysDictItem> list = sysDictItemService.list(wrapper);
        return Result.success(list);
    }

    @RequestMapping(value = "/listByCode/{code}", method = RequestMethod.GET)
    @Operation(summary = "获取字典项列表", description = "获取字典项列表")
    public Result<List<SysDictItem>> getItemByCode(
            @Parameter(description = "查询参数") @PathVariable("code") String code) {
        if (code == null) {
            throw new BizException("字典编码不能为空");
        }
        List<SysDictItem> list = sysDictItemService.getItemByCode(code);
        return Result.success(list);
    }

    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public Result<SysDictItem> queryById(@PathVariable("id") Long id) {
        SysDictItem item = sysDictItemService.getById(id);
        return Result.success(item);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Operation(summary = "新增字典", description = "新增字典")
    public Result<?> add(
            @Parameter(description = "系统用户") @RequestBody SysDictItem item) {
        sysDictItemService.add(item);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @Operation(summary = "更新字典", description = "更新字典")
    public Result<?> update(
            @Parameter(description = "系统用户") @RequestBody SysDictItem item) {
        sysDictItemService.updateItem(item);
        return Result.success("更新成功");
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @Operation(summary = "批量删除字典项", description = "批量删除字典项")
    public Result<?> batchDel(
            @Parameter(description = "删除ID") @RequestParam("ids") List<Long> ids) {
        sysDictItemService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }


    /**
     * 刷新字典缓存
     */
    @RequestMapping(value = "/refreshCache", method = RequestMethod.GET)
    @Operation(summary = "刷新字典缓存", description = "刷新字典缓存")
    public Result<?> refreshCache() {
        sysDictItemService.resetDictCache();
        return Result.success("操作成功");
    }
}
